package chpt05;

import java.util.Arrays;

class Sheep implements Cloneable {
	private String name;
	private int age;
	private String[] alias;

	public Sheep(String name, int age, String[] alias) {
		this.name = name;
		this.age = age;
		this.alias = alias;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String[] getAlias() {
		return alias;
	}

	public void setAlias(String[] alias) {
		this.alias = alias;
	}

	public void show() {
		System.out.print("Sheep:" + hashCode() + 
				", name=" + name + ", age=" + age);
		System.out.println(", alias: " + Arrays.toString(alias));
	}

	@Override
	public Sheep clone() throws CloneNotSupportedException {
		/*Sheep cloned = (Sheep) super.clone();
		String[] clonedAlias = alias.clone();
		cloned.setAlias(clonedAlias);
		return cloned;*/
		return (Sheep) super.clone();
	}

}

public class CloneableTest {

	public static void main(String[] args) throws CloneNotSupportedException {
		String[] alias = { "多利", "Duo Li" };
		Sheep s = new Sheep("Dolly", 2, alias);
		Sheep c = s.clone();
		s.show();c.show();
		String[] aliasC = c.getAlias();
		aliasC[1] = "多莉"; // 改动了s中alias中的内容
		c.setAge(3);       // 并未改动s中的age 
		s.show();c.show();
	}

}
